/*
 * openTCS copyright information:
 * Copyright (c) 2005-2011 ifak e.V.
 * Copyright (c) 2012 Fraunhofer IML
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.guing.transport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static java.util.Objects.requireNonNull;
import java.util.Vector;
import java.util.stream.Collectors;
import javax.swing.DefaultComboBoxModel;
import org.opentcs.data.order.DriveOrder;
import org.opentcs.guing.components.dialogs.DialogContent;
import org.opentcs.guing.model.ModelComponent;
import org.opentcs.guing.model.elements.LocationModel;
import org.opentcs.guing.model.elements.LocationTypeModel;
import org.opentcs.guing.util.I18nPlantOverview;
import org.opentcs.guing.util.ResourceBundleUtil;

/**
 * Eine Benutzerschnittstelle zum Auswählen einer Station oder eines
 * Meldepunktes als direktes Fahrtziel für ein Fahrzeug.
 *
 * @author Sebastian Naumann (ifak e.V. Magdeburg)
 */
public class LocationActionPanel
    extends DialogContent {

  /**
   * Die zur Auswahl stehenden Stationen.
   */
  protected List<LocationModel> fLocations;
  /**
   * Die zur Auswahl stehenden Aktionen.
   */
  protected List<String> fActions;

  /**
   * Creates new instance.
   *
   * @param locations The location models.
   */
  public LocationActionPanel(List<LocationModel> locations) {
    this.fLocations = requireNonNull(locations, "locations");

    initComponents();

    setDialogTitle(ResourceBundleUtil.getBundle(I18nPlantOverview.VEHICLEPOPUP_PATH).getString("locationActionPanel.title"));

    Collections.sort(fLocations, getComparator());

    List<String> names = fLocations.stream()
        .map(location -> location.getName())
        .collect(Collectors.toList());

    locationsComboBox.setModel(new DefaultComboBoxModel<>(new Vector<>(names)));

    updateActions();
  }

  /**
   * Liefert einen Comparator zum Sortieren der Elemente.
   *
   * @return den Comparator
   */
  protected final Comparator<ModelComponent> getComparator() {
    return (ModelComponent item1, ModelComponent item2) -> {
      String s1 = item1.getName();
      String s2 = item2.getName();
      s1 = s1.toLowerCase();
      s2 = s2.toLowerCase();

      return s1.compareTo(s2);
    };
  }

  /**
   * Liefert das ausgewählte Ziel, zu dem das Fahrzeug hinbeordert werden soll.
   *
   * @return das Ziel oder
   * <code>null</code>, falls kein Ziel ausgewählt wurde
   */
  public LocationModel getSelectedLocation() {
    int index = locationsComboBox.getSelectedIndex();

    if (index == -1) {
      return null;
    }

    return fLocations.get(index);
  }

  public String getSelectedAction() {
    int index = actionsComboBox.getSelectedIndex();

    if (index == -1) {
      return null;
    }

    return fActions.get(index);
  }

  /**
   * Zu der selektierten Location die passenden Actions anzeigen
   */
  private void updateActions() {
    LocationModel selectedLocation = getSelectedLocation();
    LocationTypeModel locationType = selectedLocation.getLocationType();
    List<String> actions = locationType.getPropertyAllowedOperations().getItems();
    Collections.sort(actions);
    // "No Operation" immer als erste Aktion anzeigen
    fActions = new ArrayList<>();
    fActions.add(DriveOrder.Destination.OP_NOP);
    fActions.addAll(actions);
    actionsComboBox.setModel(new DefaultComboBoxModel<>(new Vector<>(fActions)));
  }

  @Override
  public void update() {
  }

  @Override
  public void initFields() {
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    locationsLabel = new javax.swing.JLabel();
    locationsComboBox = new javax.swing.JComboBox<>();
    actionsLabel = new javax.swing.JLabel();
    actionsComboBox = new javax.swing.JComboBox<>();

    setPreferredSize(new java.awt.Dimension(150, 40));
    setLayout(new java.awt.GridBagLayout());

    locationsLabel.setFont(locationsLabel.getFont());
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/org/opentcs/plantoverview/dialogs/vehiclePopup"); // NOI18N
    locationsLabel.setText(bundle.getString("locationActionPanel.label_location.text")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
    add(locationsLabel, gridBagConstraints);

    locationsComboBox.setFont(locationsComboBox.getFont());
    locationsComboBox.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        locationsComboBoxItemStateChanged(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.weightx = 0.5;
    add(locationsComboBox, gridBagConstraints);

    actionsLabel.setFont(actionsLabel.getFont());
    actionsLabel.setText(bundle.getString("locationActionPanel.label_action.text")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
    add(actionsLabel, gridBagConstraints);

    actionsComboBox.setFont(actionsComboBox.getFont());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add(actionsComboBox, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

  private void locationsComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_locationsComboBoxItemStateChanged
    updateActions();
  }//GEN-LAST:event_locationsComboBoxItemStateChanged

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JComboBox<String> actionsComboBox;
  private javax.swing.JLabel actionsLabel;
  private javax.swing.JComboBox<String> locationsComboBox;
  private javax.swing.JLabel locationsLabel;
  // End of variables declaration//GEN-END:variables
}
